package com.lg.erp.controller.wangyu;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lg.erp.entity.JsonResult;
import com.lg.erp.entity.SUser;
import com.lg.erp.entity.chenjunhui.TOrderC;
import com.lg.erp.entity.chenjunhui.TProductHandover;
import com.lg.erp.entity.wangyu.TDepartmentWy;
import com.lg.erp.entity.wangyu.WyTProduct;
import com.lg.erp.entity.wangyu.TPurchaseOrder;
import com.lg.erp.entity.wangyu.TStaffWy;
import com.lg.erp.service.IUserService;
import com.lg.erp.service.chenjunhui.ITProductHandoverService;
import com.lg.erp.service.wangyu.ITPurchaseOrderService;
import com.lg.erp.service.wangyu.WyTProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
public class procurementController  {

    @Autowired
    private ITPurchaseOrderService service;
    @Autowired
    private IUserService userService;
    @Autowired
    private ITProductHandoverService productHandoverService;

    @Autowired
    private WyTProductService services;

    private static SUser one;

    private static String id;

    //添加采购申请
    @ResponseBody
    @RequestMapping(value = "/apply",method = RequestMethod.POST)
    public JsonResult saveList(@RequestBody TPurchaseOrder tPurchaseOrder){
        tPurchaseOrder.setId(UUID.randomUUID().toString().replace("-",""));
        tPurchaseOrder.setApplicationStatus("2");
        tPurchaseOrder.setTransactionNumber(UUID.randomUUID().toString().replace("-",""));

        Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(principal instanceof User){
            User user=(User) principal;
            one = userService.getOne(new QueryWrapper<SUser>().lambda().eq(SUser::getUsername, user.getUsername()));
        }
        service.save(tPurchaseOrder);
        return new JsonResult(1,"申请表添加成功",tPurchaseOrder);
    }

    //加载员工表和公司表
    @ResponseBody
    @RequestMapping("/searchWy")
    public JsonResult getStaff(Model model){
        List<TStaffWy> staffs=service.getStaffs();
        model.addAttribute("staffs",staffs);//员工
        List<TDepartmentWy> departments=service.getDepartments();
        model.addAttribute("departments",departments);//部门
        List<TDepartmentWy> departmentsName=service.getDepartmentNames();
        model.addAttribute("departmentsName",departmentsName);//部门名称
        return new JsonResult(1,"查找成功",model);
    }

    //加载采购申请列表
    @ResponseBody
    @RequestMapping("/applyList/{current}")
    public JsonResult getApplyList(@PathVariable Integer current){
        Page<TPurchaseOrder> page = new Page<>(current, 10);
        IPage<TPurchaseOrder> pages = service.getOrderByPage(page);
        return new JsonResult(1,"suprise",pages);
    }

    //加载供应商产品
    @ResponseBody
    @RequestMapping("/purchaseProduct/{current}")
    public JsonResult getProduct(@PathVariable Integer current){
        Page<WyTProduct> page=new Page<>(current,10);
        IPage<WyTProduct> page1=service.getAll(id,page);
        return new JsonResult(1,"ok",page1);
    }


    //通过产品id查找产品
    @ResponseBody
    @RequestMapping("/purchase/Product/{id}")
    public JsonResult getProductById(@PathVariable String id){
        WyTProduct product = services.getProductById(id);
        return new JsonResult(1,"123",product);
    }

    @RequestMapping("/goPurchase/{companyId}")
    public String goPurchasePage(@PathVariable String companyId){
        id=companyId;
        return "/buy/into";
    }

    //审核驳回
    @ResponseBody
    @RequestMapping(value = "/refuseAudit",method = RequestMethod.PUT)
    public JsonResult refuseAudit(@RequestBody TPurchaseOrder order){
        service.alterById(order);
        return new JsonResult(1,"qwe",order);
    }

    @ResponseBody
    @RequestMapping(value = "/passAudit",method = RequestMethod.PUT)
    public JsonResult passAudit(@RequestBody TPurchaseOrder order){
        service.alterStatusById(order);
        return new JsonResult(1,"12",order);
    }
}
